import { Injectable } from '@nestjs/common';
import { Strategy } from 'passport-local'
import { PassportStrategy } from '@nestjs/passport'
import { CommandBus } from '@nestjs/cqrs';
import { ValidateUserLocalCommand } from '../commands/validate-user-local.command';

@Injectable()
export class AuthLocalStrategy extends PassportStrategy(Strategy, 'auth-local') {
  constructor(private readonly commandBus: CommandBus) {
    super({
      // 修改字段名
      usernameField: 'username',

    })
  }
  // 验证方法 反回的就是用户信息 后续可以在上下文中获取到
  // 接收的参数根据策略的方式有所不同 本地的是username password
  validate(username: string, password: string) {
    return this.commandBus.execute(new ValidateUserLocalCommand({ username, password }))
  }
}
